package cn.sdormitory.service.impl;

import cn.sdormitory.basedata.dao.BClassDao;
import cn.sdormitory.basedata.dao.BStudentDao;
import cn.sdormitory.basedata.entity.BStudent;
import cn.sdormitory.basedata.vo.BClassVo;
import cn.sdormitory.exam.dao.ClassroomTestDao;
import cn.sdormitory.exam.dao.ExamDao;
import cn.sdormitory.exam.entity.Exam;
import cn.sdormitory.exam.vo.*;
import cn.sdormitory.service.AppExamService;
import cn.sdormitory.sys.dao.SysDeptDao;
import cn.sdormitory.sys.dao.SysUserDao;
import cn.sdormitory.sys.entity.SysUser;
import cn.sdormitory.sys.service.SysUserRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : yy
 * @date: 2024/12/30 17:01
 * @description:
 */
@Service
public class AppExamServiceImpl implements AppExamService {
    @Resource
    private ExamDao examDao;
    @Resource
    private ClassroomTestDao classroomTestDao;

    @Resource
    private SysUserRoleService sysUserRoleService;
    @Resource
    private BClassDao classDao;
    @Resource
    private SysUserDao sysUserDao;
    @Resource
    private BStudentDao bStudentDao;
    @Resource
    private SysDeptDao sysDeptDao;

    @Override
    public List<Exam> getExam(Integer studentId) {
        return examDao.getAppExamByStudentId(studentId);
    }
    @Override
    public List<AppStudentExamDetailVo> getExamDetail(Integer examId, Integer studentId) {
        return examDao.getAppExamDetail(examId,studentId);
    }
    @Override
    public List<Exam> getClassExam(Long userId) {
        boolean isDeptManager = false ; //是否部门经理
        List<Long> roleIdList = sysUserRoleService.listRoleIdByUserId(userId);
        for (Long roleId : roleIdList) {
            if(roleId == 2) {
                isDeptManager = true ;
                break;
            }
        }

        List<Long> classIds = new ArrayList<>();
        if (isDeptManager) {
            SysUser user = sysUserDao.selectById(userId);
            List<Long> deptIds = sysDeptDao.getAllChildByParent(user.getDeptId());
            List<BClassVo> classList = classDao.getAppClassByDeptIds(deptIds);
            classIds = classList.stream().map(BClassVo::getClassId).collect(Collectors.toList());
        } else {
            List<BClassVo> cList = classDao.getAppClassListByTeaId(userId);
            for (BClassVo bClassVo : cList) {
                LambdaQueryWrapper<BStudent> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(BStudent::getClassId, bClassVo.getClassId())
                        .eq(BStudent::getIsGraduated, "0");
                List<BStudent> studentList = bStudentDao.selectList(wrapper);
                if (!studentList.isEmpty()) {
                    classIds.add(bClassVo.getClassId());
                }
            }
        }
        return examDao.getAppClassExamByClassIds(classIds);
    }
    @Override
    public List<AppClassExamCountVo> getClassExamCount(Integer examId) {
        return null;
    }




    @Override
    public List<AppClassroomTestRecordVo> getClassRoomTestRecord(Integer studentId) {
        return classroomTestDao.getAppClassRoomTestRecord(studentId);
    }
    @Override
    public List<ClassroomTestVo> getTestCount(Long userId) {
        boolean isDeptManager = false ; //是否部门经理
        List<Long> roleIdList = sysUserRoleService.listRoleIdByUserId(userId);
        for (Long roleId : roleIdList) {
            if(roleId == 2) {
                isDeptManager = true ;
                break;
            }
        }

        List<Long> classIds = new ArrayList<>();
        if (isDeptManager) {
            SysUser user = sysUserDao.selectById(userId);
            List<Long> deptIds = sysDeptDao.getAllChildByParent(user.getDeptId());
            List<BClassVo> classList = classDao.getAppClassByDeptIds(deptIds);
            classIds = classList.stream().map(BClassVo::getClassId).collect(Collectors.toList());
        } else {
            List<BClassVo> cList = classDao.getAppClassListByTeaId(userId);
            for (BClassVo bClassVo : cList) {
                LambdaQueryWrapper<BStudent> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(BStudent::getClassId, bClassVo.getClassId())
                        .eq(BStudent::getIsGraduated, "0");
                List<BStudent> studentList = bStudentDao.selectList(wrapper);
                if (!studentList.isEmpty()) {
                    classIds.add(bClassVo.getClassId());
                }
            }
        }
        return classroomTestDao.getTestCount(classIds);
    }
}
